<!--
 * @Autor: Gct
 * @Description: 系统配置页面
 * @Date: 2022-04-20 17:00:41
 * @LastEditors: Gct
 * @LastEditTime: 2022-04-20 17:59:55
-->
<template>
  <div>
    <!-- 第一行 -->
    <el-button size="small" type="primary" @click="toAdd">新增</el-button>
    <!-- 第一行 -->
    <!-- 第二行 -->
    <div>
      <el-table size="small" :data="baseConfig" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="name" label="名称" width="180">
        </el-table-column>
        <el-table-column prop="val" label="值">
          <template slot-scope="scope">
            <span v-if="scope.row.val.indexOf('http://') === -1">{{
              scope.row.val
            }}</span>
            <img v-else width="150px" :src="scope.row.val" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="introduce" label="介绍"> </el-table-column>
        <el-table-column label="操作" align="center">
          <el-button type="text" size="small">编辑</el-button>
          <el-button type="text" size="small">删除</el-button>
        </el-table-column>
      </el-table>
    </div>
    <!-- 第二行 -->
    <!-- 模态框 -->
    <el-dialog title="收货地址" :visible.sync="visible">
      <el-form :model="form">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="介绍" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            v-model="form.introduce"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="类型" :label-width="formLabelWidth">
          <el-select v-model="type" placeholder="请选择类型">
            <el-option label="文字" value="文字"></el-option>
            <el-option label="图片" value="图片"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="type == '文字'"
          label="值"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.val"></el-input>
        </el-form-item>
        <el-form-item v-else labei="图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="visible = false">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框 -->
  </div>
</template>
<script>
import { findAll, deleteById, saveOrUpdate } from "@/api/baseConfig";
import { showFileURL, uploadFileURL } from "@/utils/config";
export default {
  data() {
    return {
      // 系统配置数据
      baseConfig: [],
      title: "",
      visible: false,
      form: {},
      formLabelWidth: "80px",
      type: "文字",
      imageUrl: "",
    };
  },
  methods: {
    /**
     * @description:查询所有配置信息
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    async findAllConf() {
      let res = await findAll();
      this.baseConfig = res.data;
    },

    /**
     * @description:图片上传成功的回调
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.val = showFileURL + res.data.slice(21);
    },

    /**
     * @description: 图片上传之前的回调
     * @param {*} file
     * @return {*}
     * @author: Gct
     */
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    /**
     * @description: 新增
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    toAdd() {
      this.visible = true;
      this.title = "新增数据";
    },
  },
  created() {
    this.findAllConf();
  },
  watch: {
    type() {
      this.form.val = "";
      this.imageUrl = "";
    },
  },
};
</script>
<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>